/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package log2excel.model;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.ListIterator;

/**
 *
 * @author Wahyu Fahmy
 */
public class OutgoingCallReport {
    public int srn;
    public int stn;
    public int trk;
    public String calledNo;
    public Date date;
    public Date time;
    public int duration;
    public int unit;
    public double amount;
    public String r;

    public OutgoingCallReport(int srn, int stn, int trk, String calledNo, Date date, Date time, int duration, int unit, double amount, String r){
        this.srn = srn;
        this.stn = stn;
        this.trk = trk;
        this.calledNo = calledNo;
        this.date = date;
        this.time = time;
        this.duration = duration;
        this.unit = unit;
        this.amount = amount;
        this.r = r;
    }

    public Object[] toArray(){
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat tf = new SimpleDateFormat("HH:mm:ss");
        return new Object[]{
            this.srn,
            this.stn,
            this.trk,
            this.calledNo,
            df.format(this.date),
            tf.format(this.time),
            this.duration,
            this.unit,
            this.amount,
            this.r
        };
    }
    
    public static boolean matchByExtension(OutgoingCallReport data, Date datestart, Date dateend, 
            long timestart, long timeend, int duration, ArrayList<Integer> stn){
        boolean found = false;
        long start = datestart.getTime() + timestart;
        long end = dateend.getTime() + timeend;
        long data_datetime = data.date.getTime() + data.time.getTime();
        
        System.out.println("DEBUG TIME" + start + " " + end + " " + data_datetime);
        if ( (data_datetime > start) && (data_datetime < end) && (data.duration > duration)){
            int i = 0;
            while (i<stn.size() && !found) {
                if (stn.get(i) == data.stn){
                    found = true;
                }
                i++;
            }
        }
        
        return found;
    }
    
    public static boolean matchByTrunk(OutgoingCallReport data, Date datestart, Date dateend, 
            long timestart, long timeend, int duration, ArrayList<Integer> trunk){
        boolean found = false;
        long start = datestart.getTime() + timestart;
        long end = dateend.getTime() + timeend;
        long data_datetime = data.date.getTime() + data.time.getTime();
        
        if ( (data_datetime > start) && (data_datetime < end) && (data.duration > duration)){
            int i = 0;
            while (i<trunk.size() && !found) {
                if (trunk.get(i) == data.trk){
                    found = true;
                }
                i++;
            }
        }
        
        return found;
    }

    public static ArrayList<OutgoingCallReport> Records = new ArrayList<OutgoingCallReport>();

    public static ArrayList<OutgoingCallReport> search(Date datestart, Date dateend, long timestart, long timeend,
            int duration, int mode, ArrayList<Integer> comparation){
        
        // if mode = 1, means it is "Username/extension" and comparation is list of extension
        // if mode = 2, means it is "Trunk/line" and comparation is list of trunk
        
        ArrayList<OutgoingCallReport> result = new ArrayList<OutgoingCallReport>();
        
        ListIterator<OutgoingCallReport> litr = OutgoingCallReport.Records.listIterator();
        while (litr.hasNext()) {
            OutgoingCallReport contentoutgoing = litr.next();
            if (mode == 1) {
                // by extension
                if (OutgoingCallReport.matchByExtension(contentoutgoing, datestart, dateend, timestart, timeend, 
                        duration, comparation)){
                    result.add(contentoutgoing);
                }
            } else {
                // by trunk
                if (OutgoingCallReport.matchByTrunk(contentoutgoing, datestart, dateend, timestart, timeend, 
                        duration, comparation)){
                    result.add(contentoutgoing);
                }
            }
        }
        return result;
//        return OutgoingCallReport.Records;
    }
}
